# -*- coding: utf-8 -*-
from enum import Enum

# https://www.liaoxuefeng.com/article/994977272296736
class Status(object):
    STATUS_EXPECT_OBJECT_KEY = 1
    STATUS_EXPECT_SINGLE_VALUE = 2
    STATUS_EXPECT_BEGIN_OBJECT = 4
    STATUS_EXPECT_END_OBJECT = 8
    STATUS_EXPECT_SPACE = 16
    
    
class TokenCode(object):
    SEP_COMMA = 1
    SEP_SPACE = 2
    SEP_SINGLE_QUOTE = 4
    SEP_DOUBLE_QUOTE = 8
    STRING = 16
    BEGIN_OBJECT = 32
    END_OBJECT = 64
    pass

class TokenStr(object):
    SEP_COMMA = ':'
    SEP_SPACE = ' '
    SEP_SINGLE_QUOTE = '\''
    SEP_DOUBLE_QUOTE = '\"'
    SEP_STOP = '.'
    SEP_HYPHEN = '-'
    BEGIN_OBJECT = '{'
    END_OBJECT = '}'
    pass
        
    
class TokenReader(object):
    
    def __init__(self, filename):
        self.filename = filename
        self.text = ''
        self.index = 0
        
        self.APPLY_PLUGIN = 'apply plugin'
        self.SEP_COMMA = ':'
        with open(self.filename, "r", encoding = 'utf-8') as f:
             self.text = f.read()
             self.text = self.text.replace('\n','')
        pass
    
    def readNextToken(self):
        
        indexTmp = 0
        if self.index >= len(self.text):
            return ''
        char = self.text[self.index]
            
        if char == TokenStr().SEP_COMMA:
            self.index += 1
            return TokenCode().SEP_COMMA
        
        elif char == TokenStr().BEGIN_OBJECT:
            self.index += 1
            return TokenCode().BEGIN_OBJECT
        
        elif char == TokenStr().END_OBJECT:
            self.index += 1
            return TokenCode().END_OBJECT
        elif char == TokenStr().SEP_SPACE:
            self.index += 1
            return TokenCode().SEP_SPACE
        else:
            return TokenCode().STRING
        # print(char)
        pass
    
    def checkIf(self,key,index):
        checkResult = {}
        result = False
        str1 = ''
        for idx in range(index,index + len(key)):
            str1 += self.text[idx]
        
        checkResult['token'] = str1
        checkResult['len'] = len(key)
        checkResult['result'] = str1 == key
        return checkResult
        pass
    
    def readString(self):
        str1 = ''
        while(True):
            c = self.text[self.index]
            if c.isdigit() or \
                c.isalpha() or \
                c == TokenStr().SEP_SINGLE_QUOTE or \
                c == TokenStr().SEP_DOUBLE_QUOTE or \
                c == TokenStr().SEP_HYPHEN or \
                c == TokenStr().SEP_STOP:
               str1 += c
               self.index += 1
            else:
               break
        return str1        
        pass
    
    def readObject(self):
        pass
    
    def isLetter(self,char):
        return char.isdigit()
        pass

class GrovvyParser(object):
    
    def __init__(self, filename):
        self.filename = filename
        pass
    
    def parse(self):
        
        pass
   
class Type(object):
    TypeKey = 1
    TypeValue = 2
    TypeBeginObject = 3
    TypeEndObject = 4
    
class TypeValueBean(object):
    
    def __init__(self):
        self.type = None
        self.value = None
        
    def __str__(self):
        return "type=%s,value=%s" % (self.type, self.value)
    
tokenReader = TokenReader('build1.gradle')

tree = {}
current = {}
stack = []

key = ''

status = Status.STATUS_EXPECT_SINGLE_VALUE | Status.STATUS_EXPECT_BEGIN_OBJECT | Status.STATUS_EXPECT_OBJECT_KEY
for i in range(0,150):
    token = tokenReader.readNextToken()
    if token == TokenCode().STRING:
        if status & Status.STATUS_EXPECT_OBJECT_KEY != 0:
            key = tokenReader.readString()
            status = Status.STATUS_EXPECT_SPACE | Status.STATUS_EXPECT_SINGLE_VALUE | Status.STATUS_EXPECT_BEGIN_OBJECT
            continue
        if status & Status.STATUS_EXPECT_SINGLE_VALUE != 0:
            value = tokenReader.readString()
            current[key] = value
            status = Status.STATUS_EXPECT_SPACE | Status.STATUS_EXPECT_OBJECT_KEY | Status.STATUS_EXPECT_BEGIN_OBJECT | Status.STATUS_EXPECT_END_OBJECT
            continue
    elif token == TokenCode().SEP_SPACE:
        if status & Status.STATUS_EXPECT_SPACE != 0:
            if status &  Status.STATUS_EXPECT_SINGLE_VALUE != 0:
                status = Status.STATUS_EXPECT_SINGLE_VALUE | Status.STATUS_EXPECT_BEGIN_OBJECT | Status.STATUS_EXPECT_SPACE | Status.STATUS_EXPECT_END_OBJECT
            else:
                status = Status.STATUS_EXPECT_OBJECT_KEY | Status.STATUS_EXPECT_BEGIN_OBJECT | Status.STATUS_EXPECT_SPACE | Status.STATUS_EXPECT_END_OBJECT
        continue
    elif token == TokenCode().BEGIN_OBJECT:
        if status & Status.STATUS_EXPECT_BEGIN_OBJECT != 0:
            status = Status.STATUS_EXPECT_OBJECT_KEY | Status.STATUS_EXPECT_SPACE | Status.STATUS_EXPECT_END_OBJECT
            current = {}
            current[key] = {}
            stack.append(current)
        continue
    elif token == TokenCode().END_OBJECT:
        if status & Status.STATUS_EXPECT_END_OBJECT != 0:
            status = Status.STATUS_EXPECT_OBJECT_KEY | Status.STATUS_EXPECT_BEGIN_OBJECT
            if len(stack) == 1:
                root = stack.pop()
                tree = root
            else:
                stack.pop()
            # print(key.value)
        continue
    
        
print(tree)